//
//  SchemeHandler.m
//  goldmetal
//
//  Created by mac on 2017/1/19.
//  Copyright © 2017年 宏鹿. All rights reserved.
//

#import "SchemeHandler.h"
#import "SchemeDefine.h"
#import "VCManager.h"


@interface SchemeHandler ()

@property(nonatomic, strong) UIViewController *topWebController;

@end


@implementation SchemeHandler

static SchemeHandler *m_handler;
+ (id)defaultHandler{
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        m_handler = [[SchemeHandler alloc] init];
    });
    return m_handler;
}

#pragma mark ----  public method
//判断当前链接是否是自定义url
+ (BOOL)isLocalScheme:(NSURL*)urlStr{
    if ([(NSString *)[urlStr scheme] rangeOfString:URL_SCHEME].location != NSNotFound)
    {
        return YES;
    }
    return NO;
}
//对链接进行处理
- (void)handleUrl:(NSString*)urlStr animated:(BOOL)animated{
    [self handleUrl:urlStr animated:animated config:@""];
}
//对连接进行处理
- (void)handleUrl:(NSString*)urlStr animated:(BOOL)animated config:(NSString *)config{

    //先判断是否是以我们定义的scheme开始
    if (![SchemeHandler isLocalScheme:[NSURL URLWithString:urlStr]]){
        return;
    }
    
    //根据url,获取path类型:处理web和native
    HandleType type = [self getHandleType:urlStr];
    
    switch (type){
        case TYPE_OPEN_WEB:
            [self handleOpenWeb:urlStr animated:animated config:config];
            break;
        case TYPE_OPEN_NATIVE:
            [self handleOpenNative:urlStr animated:animated config:config];
            break;
        case TYPE_OPEN_SHARE:
            [self handleOpenShare:urlStr animated:animated config:config];
            break;
        case TYPE_OPEN_CLOSE:
            [self handleOpenClose:urlStr animated:animated config:config];
            break;
        case TYPE_OPEN_NATIVENEW:
            [self handleOpenNativeNew:urlStr animated:animated config:config];
            break;
        case TYPE_OPEN_WXGUANZHU:
            // 以后再写
            // [self wxGuanzhu];
            break;
        case TYPE_INVALID:
            return;
    }
    
}

#pragma mark - private method
- (HandleType)getHandleType:(NSString*)urlStr{
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    
//    NSString *xiaoniuValue = [url getValueInQueryForKey:URL_QUERY_KEY_NEED_XIAONIULOGIN isCaseSensitive:NO];
//    NSString *ggValue = [url getValueInQueryForKey:URL_QUERY_KEY_NEED_GGLOGIN isCaseSensitive:NO];
//    NSString *jnValue = [url getValueInQueryForKey:URL_QUERY_KEY_NEED_JNLOGIN isCaseSensitive:NO];
//    NSString *hgValue = [url getValueInQueryForKey:URL_QUERY_KEY_NEED_HGLOGIN isCaseSensitive:NO];
//    //需要先登入的情况，进入登入界面，登入成功后回调回来
//    if ([xiaoniuValue isEqualToString:@"1"]) {
//        if (![HttpTool isXiaoniuLogin]) {
//            [HttpTool loginAction];
//            return TYPE_INVALID;
//        }
//    }else if ([ggValue isEqualToString:@"1"]) {
//        if (![HttpTool isGgLogin]) {
//            [HttpTool ggLoginAction];
//            return TYPE_INVALID;
//        }
//    }else if ([hgValue isEqualToString:@"1"]) {
//        if (![HttpTool isHgLogin]) {
//            [HttpTool hgLoginAction];
//            return TYPE_INVALID;
//        }
//    }else if ([jnValue isEqualToString:@"1"]) {
//        if (![HttpTool isJnLogin]) {
//            [HttpTool jnLoginAction];
//            return TYPE_INVALID;
//        }
//    }
    

    
    //先判断域名是否是我们定义的域名
    if ([url.host isEqualToString:URL_HOST]) {
        
        NSString *path = url.path;
        //再判断路径
        if ([path isEqualToString:URL_PATH_WEB]){
            // /jump
            return TYPE_OPEN_WEB;
        }
        else if ([path isEqualToString:URL_PATH_NATIVE]){
            // /native
            return TYPE_OPEN_NATIVE;
        }
        else if ([path isEqualToString:URL_PATH_SHARE]){
            // /share
            return TYPE_OPEN_SHARE;
        }
        else if ([path isEqualToString:URL_PATH_CLOSR]){
            // /close
            return TYPE_OPEN_CLOSE;
        }
        else if ([path isEqualToString:URL_PATH_NATIVENEW]){
            // /native_new
            return TYPE_OPEN_NATIVENEW;
        }
        else if ([path isEqualToString:URL_PATH_WXGUANZHU]){
            // /wx_guanzhu
            return TYPE_OPEN_WXGUANZHU;
        }
    }
    return TYPE_INVALID;
    
}


#pragma mark - 协议处理
//处理jump协议
- (void)handleOpenWeb:(NSString*)urlStr animated:(BOOL)animated config:(NSString *)config
{
    UIViewController *webVC = [self generateWebVC:urlStr config:config];
    webVC.hidesBottomBarWhenPushed = YES;
    self.topWebController = webVC;
    
    UIViewController *currentVC = [[VCManager shareVCManager] getTopViewController];
    if (currentVC.navigationController.navigationBarHidden) {
        currentVC.navigationController.navigationBarHidden = NO;
    }
    [currentVC.navigationController pushViewController:webVC animated:animated];
}

//处理native协议
- (void)handleOpenNative:(NSString *)urlStr animated:(BOOL)animated config:(NSString *)config
{
    UIViewController *nativeVC = [self generateNativeVC:urlStr config:config];
    if (nativeVC) {
        nativeVC.hidesBottomBarWhenPushed = YES;
        
        UIViewController *currentVC = [[VCManager shareVCManager] getTopViewController];
        
        [currentVC.navigationController pushViewController:nativeVC animated:animated];
    }
}

//处理share协议
- (void)handleOpenShare:(NSString *)urlStr animated:(BOOL)animated config:(NSString *)config
{

}

//处理close协议
- (void)handleOpenClose:(NSString *)urlStr animated:(BOOL)animated config:(NSString *)config
{
    //关闭当前webVC
    if (self.topWebController.presentingViewController) {
        [self.topWebController dismissViewControllerAnimated:YES completion:nil];
    } else {
        NSMutableArray *mArr = [NSMutableArray arrayWithArray:self.topWebController.navigationController.viewControllers];
        for (UIViewController *vc in mArr) {
            if ([vc isKindOfClass:[self.topWebController class]]) {
                [mArr removeObject:vc];
                break;
            }
        }
        self.topWebController.navigationController.viewControllers = mArr;
    }
}

//处理native_new协议
- (void)handleOpenNativeNew:(NSString *)urlStr animated:(BOOL)animated config:(NSString *)config
{
    UIViewController *nativeVC = [self generateNativeNewVC:urlStr config:config];
    if (nativeVC) {
        nativeVC.hidesBottomBarWhenPushed = YES;
        
        UIViewController *currentVC = [[VCManager shareVCManager] getTopViewController];
        
        [currentVC.navigationController pushViewController:nativeVC animated:animated];
    }
}

#pragma mark - 创建ViewController
//生成显示H5页面的ViewController
- (UIViewController *)generateWebVC:(NSString *)urlStr config:(NSString *)config
{
    return nil;
}

- (UIViewController *)generateNativeVC:(NSString *)urlStr config:(NSString *)config
{
    return nil;
}

//解析url，获取native界面控制器名称，通过反射生产controller
- (UIViewController *)generateNativeNewVC:(NSString *)urlStr config:(NSString *)config
{
    return nil;
}








@end













